package com.gexin.model.integral;

import com.gexin.common.utils.SQLUtils;
import com.gexin.model.integral.base.BaseUserIntegralRecord;
import com.gexin.model.system.AdminConfig;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 业务名称:  t_user_integral_record(用户积分流水表) 
 */
@SuppressWarnings("serial")
public class UserIntegralRecord extends BaseUserIntegralRecord<UserIntegralRecord> {
	public static final UserIntegralRecord dao = new UserIntegralRecord();
	/** 类型 1 增加积分*/
	public static final Integer TYPEADD = 1;
	/** 类型 2 减少积分*/
	public static final Integer TYPESUB = 2;

	public Page<UserIntegralRecord> query(int pageNumber,int pageSize,UserIntegralRecord record){
		List<Object> list =new LinkedList<Object>();
		SQLUtils sql=new SQLUtils(" from t_user_integral_record where 1=1 ");
		if(record!=null){
			if(record.getUserId()!=null){
				sql.append(" and user_id = ? ");
				list.add(record.getUserId());
			}
		}
		sql.append(" order by create_time desc ");
		return paginate(pageNumber,pageSize,"select * ",sql.toString(),list.toArray());
	}

	public List<UserIntegralRecord> findByUserAll(Integer user_id){
		return dao.find("select * from t_user_integral_record where user_id = ?  order by create_time desc ",user_id);
	}

	public void saveLog(Integer integral, Integer type,String reason,Integer userId) {
		UserIntegralRecord userIntegralRecord = new UserIntegralRecord();
		userIntegralRecord.setCreateTime(new Date());
		userIntegralRecord.setIntegralQuantity(integral);
		userIntegralRecord.setIntegralType(type);
		userIntegralRecord.setIntegralReason(reason);
		userIntegralRecord.setUserId(userId);
		userIntegralRecord.save();

	}
}
